package com.jxj.GUI;

import com.jxj.mapper.mapperUser;
import com.jxj.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

class change extends JFrame {
    private SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
    mapperUser mapperuser = sqlSession.getMapper(mapperUser.class);
    List<JTextField> textFieldList = new ArrayList<>();
    newmain n1 = new newmain();

    public void changeWindow(JTable table) {
        JFrame frame = new JFrame("修改学生信息");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        int DIALOG_WHITE = 600;//宽度
        int DIALOG_HEIGHT = 600;//高度
        Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        frame.setBounds(point.x - DIALOG_WHITE / 2, point.y - DIALOG_HEIGHT / 2, DIALOG_WHITE, DIALOG_HEIGHT);
        frame.setVisible(true);
        frame.setIconImage(new ImageIcon("C:\\Users\\Lenovo\\Desktop\\icon.png").getImage());
        String[] list = {"学生编号", "学生姓名", "年龄", "专业", "性别", "本学期公里数"};
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(15, 15, 15, 15); // 设置组件之间的间距
        // 插入六行
        for (int i = 0; i < 6; i++) {
            JLabel label = new JLabel(list[i]);
            JTextField textField = new JTextField(20);
            textFieldList.add(textField);
            textField.setBorder(null);
            textField.setPreferredSize(new Dimension(200, 30)); // 设置文本框大小为200x30
            textField.setMargin(new Insets(5, 5, 5, 5));
            // 添加标签和文本框到主容器中
            gbc.gridy = i;
            mainPanel.add(label, gbc);
            gbc.gridx = 1;
            mainPanel.add(textField, gbc);
            // 重置横向位置
            gbc.gridx = 0;
        }
        JButton confirmButton = new JButton("确定");
        add a1 = new add();
        a1.changeButton(confirmButton);
        int row = table.getSelectedRow();
// 获取id并设置文本框的值
        Object idObj = table.getValueAt(row, 0);
        int id = idObj != null ? (int) idObj : 0;
        textFieldList.get(0).setText(String.valueOf(id));

// 获取name并设置文本框的值
        Object nameObj = table.getValueAt(row, 1);
        String name = nameObj != null ? (String) nameObj : "";
        textFieldList.get(1).setText(name);

// 获取age并设置文本框的值
        Object ageObj = table.getValueAt(row, 2);
        int age = ageObj != null ? (int) ageObj : 0;
        textFieldList.get(2).setText(String.valueOf(age));

// 获取major并设置文本框的值
        Object majorObj = table.getValueAt(row, 3);
        String major = majorObj != null ? (String) majorObj : "";
        textFieldList.get(3).setText(major);

// 获取gender并设置文本框的值
        Object genderObj = table.getValueAt(row, 4);
        String gender = genderObj != null ? (String) genderObj : "";
        textFieldList.get(4).setText(gender);

// 获取KM并设置文本框的值
        Object KMObj = table.getValueAt(row, 5);
        int KM = KMObj != null ? (int) KMObj : 0;
        textFieldList.get(5).setText(String.valueOf(KM));
        confirmButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(textFieldList.get(0).getText());
                String b = textFieldList.get(1).getText();
                int c = Integer.parseInt(textFieldList.get(2).getText());
                String d = textFieldList.get(3).getText();
                String E = textFieldList.get(4).getText();
                mapperuser.changeU1(a, b, c, d, E, id);
                sqlSession.commit();
                int a1 = Integer.parseInt(textFieldList.get(5).getText());
                int a2 = Integer.parseInt(textFieldList.get(0).getText());
                mapperuser.changeU2(a1, a2, id);
                sqlSession.commit();
                frame.setVisible(false);
                JOptionPane.showMessageDialog(
                        frame, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                        "修改成功,数据已更新",
                        "提示",
                        JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                );
            }
        });
        gbc.gridx = 1;
        gbc.gridy = 6; // 最后一行
        gbc.anchor = GridBagConstraints.LAST_LINE_END; // 设置对齐方式为最后一行的右下角
        mainPanel.add(confirmButton, gbc);
        // 将主容器添加到窗口中
        frame.add(mainPanel);
    }
}
